package com.hnkywl.sts.action.report;

import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.service.report.BgjhdxlService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.POIUtil;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 13-8-6
 * Time: 下午9:28
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/report/bgjhdxl")
public class BgjhdxlAction extends SimpleActionSupport{
    private BgjhdxlService bgjhdxlService;
    private Integer nf;
    private Integer yf;
    private String jhlx;
    private Long jhdw;
    private Long gyz;
    private String wzbm;
    private String jhsm;
    private Integer page;
    private Integer rows;
    private Gsjg zgs;

    @Action("bgjhdxl")
    public String bgjhdxl(){
        zgs = YgService.getCurrentYg().getZggs();
        Calendar c = Calendar.getInstance();
        nf = c.get(Calendar.YEAR);
        yf = c.get(Calendar.MONTH)+1;
        return SUCCESS;
    }
    @Action("bgjhdxlList")
    public void bgjhdxlList(){
        renderJson(bgjhdxlService.bgjhdxlList(nf,yf,jhlx,jhdw,gyz,wzbm,jhsm,page,rows));
    }

    @Action("existsJhsm")
    public void existsJhsm(){
        renderText(bgjhdxlService.existsJhsm(jhsm));
    }

    @Action("bgjhdxlExp")
    public String bgjhdxlExp(){
        List<Map> rows = bgjhdxlService.bgjhdxlExp(nf,yf,jhlx,jhdw,gyz,wzbm,jhsm);
        //第一步，创建一个webbook，对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("sheet1");
        POIUtil poiUtil = new POIUtil(wb);
        //设置列宽
        sheet.setDefaultRowHeightInPoints(21);
        sheet.setDefaultColumnWidth((short) 20);
        sheet.setColumnWidth((short) 3, (short) (12*256));
        sheet.setColumnWidth((short) 1, (short) (20 * 512));
        //第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFCellStyle headerStyle = poiUtil.createHeader3HSSFCellStyle(wb);
        //生成表头
        poiUtil.fillRow(sheet, headerStyle, 0, "业务科室", "计划描述", "物资名称", "物资编码", "物资规格", "状态", "采购方式", "计划提报数量", "供应站处理","",
                "业务科处理","","", "采购方式确定数量", "合同签订数量", "采购单开具数量", "到货数量", "销售数量");
        poiUtil.fillRow(sheet, headerStyle, 1, "", "", "", "", "", "", "", "", "平衡数量","提交数量",
                "平衡数量","配拨完成数量","提交采购数量", "", "", "", "", "");
        //合并单元格
        sheet.addMergedRegion(new Region((short) 0, (short) 1, (short) 0 , (short) 0));
        sheet.addMergedRegion(new Region((short) 0, (short) 1, (short) 1 , (short) 1));
        sheet.addMergedRegion(new Region((short) 0, (short) 1, (short) 2 , (short) 2));
        sheet.addMergedRegion(new Region((short) 0, (short) 1, (short) 3 , (short) 3));
        sheet.addMergedRegion(new Region((short) 0, (short) 1, (short) 4 , (short) 4));
        sheet.addMergedRegion(new Region((short) 0, (short) 1, (short) 5 , (short) 5));
        sheet.addMergedRegion(new Region((short) 0, (short) 1, (short) 6 , (short) 6));
        sheet.addMergedRegion(new Region((short) 0, (short) 1, (short) 7 , (short) 7));
        sheet.addMergedRegion(new Region((short) 0, (short) 0, (short) 8 , (short) 9));
        sheet.addMergedRegion(new Region((short) 0, (short) 0, (short) 10 , (short) 12));
        sheet.addMergedRegion(new Region((short) 0, (short) 1, (short) 13 , (short) 13));
        sheet.addMergedRegion(new Region((short) 0, (short) 1, (short) 14 , (short) 14));
        sheet.addMergedRegion(new Region((short) 0, (short) 1, (short) 15 , (short) 15));
        sheet.addMergedRegion(new Region((short) 0, (short) 1, (short) 16 , (short) 16));
        sheet.addMergedRegion(new Region((short) 0, (short) 1, (short) 17 , (short) 17));
        //表体部分
        for (int i = 2; i < rows.size() + 1; i++) {
            Map item = rows.get(i - 1);
            poiUtil.fillRow(sheet, null, i, item.get("ywkmc"), item.get("sm"), item.get("wzmc"), item.get("wzbm"), item.get("wzgg"),
                    item.get("zt"), item.get("cgfs"), item.get("jhsl"), item.get("gyzphsl"), item.get("gyztjsl"),
                    item.get("ywksphsl"), item.get("pbwcsl"), item.get("tjcgsl"), item.get("cgfsqdsl"), item.get("cghtqdsl"),
                    item.get("cgdkjsl"), item.get("cgdrksl"), item.get("xsdcksl"));
        }
        try {
            response.setContentType("application/x-download");
            String filedisplay = new String(("保供兑现率汇总表.xls").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            wb.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("位置异常，导出失败" + e.getMessage());
        }
        return null;
    }

    @Autowired
    public void setBgjhdxlService(BgjhdxlService bgjhdxlService) {
        this.bgjhdxlService = bgjhdxlService;
    }

    public Integer getNf() {
        return nf;
    }

    public void setNf(Integer nf) {
        this.nf = nf;
    }

    public Integer getYf() {
        return yf;
    }

    public void setYf(Integer yf) {
        this.yf = yf;
    }

    public String getJhlx() {
        return jhlx;
    }

    public void setJhlx(String jhlx) {
        this.jhlx = jhlx;
    }

    public Long getJhdw() {
        return jhdw;
    }

    public void setJhdw(Long jhdw) {
        this.jhdw = jhdw;
    }

    public Long getGyz() {
        return gyz;
    }

    public void setGyz(Long gyz) {
        this.gyz = gyz;
    }

    public String getWzbm() {
        return wzbm;
    }

    public void setWzbm(String wzbm) {
        this.wzbm = wzbm;
    }

    public String getJhsm() {
        return jhsm;
    }

    public void setJhsm(String jhsm) {
        this.jhsm = jhsm;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getRows() {
        return rows;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }

    public Gsjg getZgs() {
        return zgs;
    }

    public void setZgs(Gsjg zgs) {
        this.zgs = zgs;
    }
}
